package basis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Base64 {
	public static void main(String[] args) {
		char[] ch = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
				'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
				'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
				'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
				't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
				'5', '6', '7', '8', '9', '+', '/' };
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入字符串");
		String str = sc.nextLine();
		char c = str.charAt(0);
		byte[] b1 = new byte[str.length()];
		//将字符串转化十进制
		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			byte b = (byte) c;
			b1[i] = b;
		}
		StringBuffer sb = new StringBuffer();
		String[] str1 = new String[str.length()];
		//将十进制转化成二进制
		for (int i = 0; i < b1.length; i++) {
			str1[i] = Integer.toBinaryString(b1[i]);
				sb.append(0);
			sb.reverse().append(str1[i]);
		}
		System.out.println(sb);
		// 将字符串分组
		String str3 = "";
		int k = 0;
		int len = 0;
		String[] arr = new String[sb.length()];
		int x = 6;
		// arr1数组存取分组
		// 计算arr1长度
		if (sb.length() % x == 0) {
			len = sb.length() / x;
		} else {
			len = sb.length() / x + 1;
		}
		String[] arr1 = new String[len];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = sb.charAt(i) + "";
		}
		for (int i = 0; i < arr.length; i++) {
			if (i % x == 0 && i != 0) {
				arr1[k] = str3;
				str3 = "";
				k++;
			}
			str3 += arr[i];
		}
		arr1[arr1.length - 1] = str3;
		System.out.println(Arrays.toString(arr1));
		int[] str4 = new int[arr1.length];
		for (int i = 0; i < arr1.length; i++) {
			str4[i] = Integer.parseInt(arr1[i], 2);
		}

		StringBuffer sb1 = new StringBuffer();
		for (int i = 0; i < str4.length; i++) {
			sb1.append(ch[str4[i]]);
		}
		System.out.println(sb1.toString());
		System.out.println(Arrays.toString(b1));
	}
}
